package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.YeastGen;

public class YeastGenPresenter extends AbstractPresenter<YeastGen> {
	final String SINGLEITEM = "yeastGens";
	final String MULTIITEM = "yeastGenss";
	
	public ArrayList<YeastGen> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public YeastGen tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public YeastGen createItemFromJSONObject(JSONObject jsonObject){
		YeastGen item = new YeastGen();
		try{
			item.setAmountLeft(jsonObject.getDouble("yGVolume"));
			item.setBrewID(jsonObject.getInt("YGBrew"));
			item.setCellCount(jsonObject.getDouble("YGCellCount"));
			item.setDensity(jsonObject.getDouble("YGDensity"));
			item.setGeneration(jsonObject.getInt("YGGeneration"));
			item.setNotes(jsonObject.getString("YGNotes"));
			item.setOriginatingBrew(jsonObject.getInt("YGOriginatingBrew"));
			item.setViability(jsonObject.getDouble("YGViability"));
			item.setVitality(jsonObject.getDouble("YGVitality"));
			item.setYeastGenDate(timeFormat.parse(jsonObject.getString("YGDate")));
			item.setYeastGenID(jsonObject.getInt("yeastGenID"));
			item.setYeastPitched(jsonObject.getBoolean("YGPitchedFilter"));
			
			if(!jsonObject.isNull("YGBrinkID")){
				//create product if set
				BrinksPresenter presenter = new BrinksPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("YGBrinkID");
				item.setBrinkID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBrinkID(null);
			}
			
			if(!jsonObject.isNull("YGYeastID")){
				//create product if set
				YeastViewPresenter presenter = new YeastViewPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("YGYeastID");
				item.setYeastID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setYeastID(null);
			}
			
			if(!jsonObject.isNull("YGPreviousYeastGen")){
				//create product if set
				YeastGensViewPresenter presenter = new YeastGensViewPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("YGPreviousYeastGen");
				item.setPreviousYeastGenID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setPreviousYeastGenID(null);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(YeastGen item){
		try{
			JSONObject json = new JSONObject();

			json.put("yGVolume", item.getAmountLeft());
			json.put("YGBrew", item.getBrewID());
			json.put("YGCellCount", item.getCellCount());
			json.put("YGDensity", item.getDensity());
			json.put("YGGeneration", item.getGeneration());
			json.put("YGNotes", item.getNotes());
			json.put("YGOriginatingBrew", item.getOriginatingBrew());
			json.put("YGViability", item.getViability());
			json.put("YGVitality", item.getVitality());
			json.put("YGDate", getDate(item.getYeastGenDate()));
			json.put("yeastGenID", item.getYeastGenID());
			json.put("YGPitchedFilter", item.isYeastPitched());

			if(item.getBrinkID()!=null){
				final BrinksPresenter presenter = new BrinksPresenter();
				json.put("YGBrinkID", presenter.createJSON(item.getBrinkID()));
			} else {
				json.put("YGBrinkID", JSONObject.NULL);
			}

			if(item.getYeastID()!=null){
				final YeastViewPresenter presenter = new YeastViewPresenter();
				json.put("YGYeastID", presenter.createJSON(item.getYeastID()));
			} else {
				json.put("YGYeastID", JSONObject.NULL);
			}

			if(item.getPreviousYeastGenID()!=null){
				final YeastGensViewPresenter presenter = new YeastGensViewPresenter();
				json.put("YGPreviousYeastGen", presenter.createJSON(item.getPreviousYeastGenID()));
			} else {
				json.put("YGPreviousYeastGen", JSONObject.NULL);
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public YeastGen getEmptyEntity(){
		YeastGen entity = new YeastGen();
		return entity;
	}
}
